﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;

namespace DALLayer.Providers
{
    public class BookmarkDataProvider : DataAccessBase
    {
         #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="BookmarkDataProvider" /> class.
        /// </summary>
        public BookmarkDataProvider() { } 

        #endregion

        #region [Public Methods]
        /// <summary>
        /// The method inserts bookmark to table
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="articleId">article id</param>
        /// <param name="bookmarkArticleUrl">article url</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool InsertBookmark(int userId, int articleId, bool isActive)
        {
            
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    INSERT INTO BookmarkArticle(UserID, ArticleID, IsActive)
                    VALUES(@UserID, @ArticleID, @IsActive)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@UserID", userId);
                base.DBParams.Add(@"@ArticleID", articleId);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;
        }

        /// <summary>
        /// The method updates the bookmark
        /// </summary>
        /// <param name="userId">user id</param>
        /// <param name="articleId">article id</param>
        /// <param name="isActive">is active</param>
        /// <returns>true or false</returns>
        public bool UpdateBookmark(int userId, int articleId, bool isActive)
        {
            bool success = false;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class

                base.CommandTextBuilder.Append(@"
                    UPDATE BookmarkArticle
                    SET UserID = @UserID, 
                        ArticleID =  @ArticleID,
                        IsActive = @IsActive
                    WHERE (UserID = @UserID AND ArticleID = @ArticleID");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@UserID", userId);
                base.DBParams.Add(@"@ArticleID", articleId);
                base.DBParams.Add(@"@IsActive", isActive);

                //Check if it is success
                success = base.ExecuteNonQuery();

            }
            catch (Exception)
            {
                success = false;
                //Log here
            }
            //return success
            return success;

        }

        #endregion
    }
}
